package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.CenarioBase;
import br.com.tiespec.model.Util.DAOException;

@Stateless(mappedName = "CenarioBaseDAO")
public class CenarioBaseDAO extends DAO {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<CenarioBase> listarCenarioBase(int first, int pageSize, String sortField,
			TipoSortOrder sortOrder, Long idClientePortal, String nome, String fluxo) {
		List<CenarioBase> aList = new ArrayList<CenarioBase>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(CenarioBase.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			
			if (idClientePortal != null)
				criteria.add(Restrictions.eq("cliente.id", idClientePortal));
			if (isNullOrBlank(nome))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(fluxo))
				criteria.createCriteria("fluxosBase")
					.add(Restrictions.ilike("acao", "%" + fluxo + "%"));
			
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			
			aList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aList;
	}

	public Integer countCenarioBase(Long idClientePortal, String nome, String fluxo) {
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(CenarioBase.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			
			if (idClientePortal != null)
				criteria.add(Restrictions.eq("cliente.id", idClientePortal));
			if (isNullOrBlank(nome))
				criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			if (isNullOrBlank(fluxo))
				criteria.createCriteria("fluxosBase")
					.add(Restrictions.ilike("acao", "%" + fluxo + "%"));
			
			criteria.setProjection(Projections.rowCount());
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	public CenarioBase selecionar(Long id) {
		CenarioBase cbInstance = new CenarioBase();
		try {
			begin();
			cbInstance = (CenarioBase) this.session.createCriteria(CenarioBase.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}

	public void remover(Long id) throws DAOException {
		delete(CenarioBase.class, id);
	}
	
	public Integer buscarCenarioBasePorNomeECliente(String nome, Long idClientePortal, Long id){
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(CenarioBase.class)
					.add(Restrictions.eq("cliente.id", idClientePortal))
					.add(Restrictions.like("nome", nome))
					.setProjection(Projections.rowCount());
			if (id != null)
				criteria.add(Restrictions.ne("id", id));
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	public CenarioBase selecionarComResponsavel(Long id) {
		CenarioBase cbInstance = new CenarioBase();
		try {
			begin();
			cbInstance = (CenarioBase) this.session.createCriteria(CenarioBase.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("responsavelCadastro", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}
	
	public CenarioBase selecionarComFluxos(Long id) {
		CenarioBase cbInstance = new CenarioBase();
		try {
			begin();
			cbInstance = (CenarioBase) this.session.createCriteria(CenarioBase.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("fluxosBase", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}

	public CenarioBase selecionarComFluxosEResponsavel(Long id) {
		CenarioBase cbInstance = new CenarioBase();
		try {
			begin();
			cbInstance = (CenarioBase) this.session.createCriteria(CenarioBase.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("responsavelCadastro", FetchMode.JOIN)
					.setFetchMode("fluxosBase", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cbInstance;
	}

}